package com.moons.demo.aicamera.facerecognition;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.moons.demo.aicamera.facerecognition.adapter.RegedFaceRecyclerViewAdapter;
import com.moons.demo.aicamera.facerecognition.util.FaceDataUtil;
import com.moons.demo.aicamera.facerecognition.util.ThreadPool.ThreadPoolManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RegisterUserListActivity extends AppCompatActivity {
    private static final String TAG = "FaceRegisterActivity";
    private ActionBar mActionBar;

    private RegedFaceRecyclerViewAdapter mRegedFaceRecyclerViewAdapter;
    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_faceregister);

        Toolbar myToolbar = findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        mActionBar = getSupportActionBar();
        if (mActionBar != null) {
//            mActionBar.setDisplayHomeAsUpEnabled(true);
            mActionBar.setTitle("注册用户列表");
        }
        mRecyclerView = findViewById(R.id.rc_reg_user);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));


        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        mRegedFaceRecyclerViewAdapter = new RegedFaceRecyclerViewAdapter(this);
        mRecyclerView.setAdapter(mRegedFaceRecyclerViewAdapter);


        mRegedFaceRecyclerViewAdapter.updateData(BaseApplication.getInstance().getDaoSession().getUserFaceDao().loadAll());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.face_reg_list_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:


                return true;

            case R.id.action_clear_all_reg_user_data:


                new MaterialDialog.Builder(this)
                        .iconRes(R.drawable.ic_action_warning)
                        .title("请注意")
                        .content("是否要清空人脸数据库?")
                        .positiveText("确定")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {


                                ThreadPoolManager.getInstance().getBackgroundExecutorService().execute(new Runnable() {
                                    @Override
                                    public void run() {
                                        FaceDataUtil.getInstance().deleteAll();

                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                mRegedFaceRecyclerViewAdapter.updateData(BaseApplication.getInstance().getDaoSession().getUserFaceDao().loadAll());

                                            }
                                        });
                                    }
                                });
                            }
                        })
                        .show();


                return true;

            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);

        }


    }
}
